home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 22 / PC Actual CD 22.iso / progs / Netobj / netobj / t2.z / DynaButtons.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-29  |  10.5 KB  |  365 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.GridLayout;
  9. import java.awt.Image;
  10. import java.awt.LayoutManager;
  11. import java.awt.MediaTracker;
  12. import java.awt.Panel;
  13. import java.net.URL;
  14.  
  15. public class DynaButtons extends Applet {
  16.    protected int orientation;
  17.    protected static final int VERT = 0;
  18.    protected static final int HORIZ = 1;
  19.    protected int textJustification;
  20.    protected int textAlignment;
  21.    protected Image buttonImage;
  22.    protected Image highliteImage;
  23.    protected Color backgroundColor;
  24.    protected Image backgroundImage;
  25.    protected Color fontColor;
  26.    protected Color highliteFontColor;
  27.    protected Font buttonFont;
  28.    protected int numButtons;
  29.    protected String documentBase;
  30.    protected int BUTTON_WIDTH;
  31.    protected int BUTTON_HEIGHT;
  32.    Panel subPanel;
  33.    boolean subPanelUp;
  34.    private int currentButtonNum = -1;
  35.    protected int xOffset;
  36.    protected int yOffset;
  37.  
  38.    protected void getTextJustification() {
  39.       this.textJustification = Integer.parseInt(((Applet)this).getParameter("TextJustification"));
  40.    }
  41.  
  42.    private void getButtonImages() {
  43.       String imageName = this.modifyStringContext(((Applet)this).getParameter("ButtonImage"));
  44.       String highliteName = this.modifyStringContext(((Applet)this).getParameter("HighliteImage"));
  45.  
  46.       try {
  47.          this.buttonImage = ((Applet)this).getImage(new URL(imageName));
  48.          this.highliteImage = ((Applet)this).getImage(new URL(highliteName));
  49.          MediaTracker tracker = new MediaTracker(this);
  50.          tracker.addImage(this.buttonImage, 0);
  51.          tracker.addImage(this.highliteImage, 1);
  52.  
  53.          try {
  54.             tracker.waitForID(0);
  55.             tracker.waitForID(1);
  56.          } catch (InterruptedException var6) {
  57.             return;
  58.          }
  59.  
  60.          this.BUTTON_WIDTH = this.buttonImage.getWidth(this);
  61.          this.BUTTON_HEIGHT = this.buttonImage.getHeight(this);
  62.       } catch (Exception var7) {
  63.          System.out.println("Error loading Button Images");
  64.       }
  65.  
  66.    }
  67.  
  68.    protected void getBackgroundImage() {
  69.       String backgroundimage = this.modifyStringContext(((Applet)this).getParameter("BackgroundImage"));
  70.  
  71.       try {
  72.          this.backgroundImage = ((Applet)this).getImage(new URL(backgroundimage));
  73.       } catch (Exception var6) {
  74.          System.out.println("Error forming URL for background image");
  75.          return;
  76.       }
  77.  
  78.       MediaTracker tracker = new MediaTracker(this);
  79.       tracker.addImage(this.backgroundImage, 0);
  80.  
  81.       try {
  82.          tracker.waitForID(0);
  83.       } catch (InterruptedException var5) {
  84.          System.out.println("Background image loading interrupted");
  85.       }
  86.    }
  87.  
  88.    protected void actionMouseDown(PopObject popObject) {
  89.       if (popObject.getButtonURL() != null) {
  90.          ((Applet)this).getAppletContext().showDocument(popObject.getButtonURL());
  91.       } else {
  92.          if (!this.subPanelUp) {
  93.             String[] stringList = popObject.getStringList();
  94.             this.subPanel = new Panel();
  95.             this.subPanel.setLayout(new GridLayout(stringList.length, 1));
  96.             int buttonNum = popObject.getButtonNum();
  97.  
  98.             for(int i = 0; i < stringList.length; ++i) {
  99.                URL buttonURL = popObject.getURLList()[i];
  100.                PopObject po = new PopObject(-1, buttonURL, (URL[])null, (String[])null);
  101.                if (this.backgroundColor != null) {
  102.                   this.subPanel.add(new PopButton(this, po, stringList[i], this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundColor));
  103.                } else {
  104.                   this.subPanel.add(new PopButton(this, po, stringList[i], this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundImage));
  105.                }
  106.             }
  107.  
  108.             if (this.orientation == 0) {
  109.                this.subPanel.reshape(this.BUTTON_WIDTH, buttonNum * this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT * stringList.length);
  110.             } else {
  111.                this.subPanel.reshape(buttonNum * this.BUTTON_WIDTH, this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT * stringList.length);
  112.             }
  113.  
  114.             ((Container)this).add(this.subPanel);
  115.             ((Container)this).validate();
  116.             this.subPanelUp = true;
  117.             this.currentButtonNum = buttonNum;
  118.          } else {
  119.             ((Container)this).remove(this.subPanel);
  120.             this.subPanelUp = false;
  121.             if (popObject.getButtonNum() == this.currentButtonNum) {
  122.                this.subPanel = new Panel();
  123.                ((Container)this).add(this.subPanel);
  124.                ((Container)this).validate();
  125.                this.currentButtonNum = -1;
  126.             } else {
  127.                this.actionMouseDown(popObject);
  128.             }
  129.          }
  130.  
  131.       }
  132.    }
  133.  
  134.    protected int countSubEntries(int buttonNum) {
  135.       int subCount;
  136.       for(subCount = 0; ((Applet)this).getParameter("ButtonText" + buttonNum + "_" + subCount) != null; ++subCount) {
  137.       }
  138.  
  139.       return subCount;
  140.    }
  141.  
  142.    protected void getTextAlignment() {
  143.       this.textAlignment = Integer.parseInt(((Applet)this).getParameter("TextAlignment"));
  144.    }
  145.  
  146.    public void paint(Graphics g) {
  147.       if (this.backgroundImage != null) {
  148.          int imHeight = this.backgroundImage.getHeight(this);
  149.          int imWidth = this.backgroundImage.getWidth(this);
  150.          Dimension d = ((Component)this).size();
  151.          int x = (d.width + this.xOffset) / imWidth + 1;
  152.          int y = (d.height + this.yOffset) / imHeight + 1;
  153.  
  154.          for(int i = 0; i < y; ++i) {
  155.             for(int j = 0; j < x; ++j) {
  156.                g.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  157.             }
  158.          }
  159.       }
  160.  
  161.    }
  162.  
  163.    private String modifyStringContext(String param) {
  164.       if (param.startsWith(".")) {
  165.          param = param.replace('\\', '/');
  166.          param = this.documentBase + "/" + param;
  167.       } else if (param.startsWith("#")) {
  168.          param = ((Applet)this).getDocumentBase().toString() + param;
  169.       } else if (!param.startsWith("cid:") && !param.startsWith("lifn:") && !param.startsWith("java:") && !param.startsWith("irc:") && !param.startsWith("IOR:") && !param.startsWith("ilu:") && !param.startsWith("https:") && !param.startsWith("http:") && !param.startsWith("hdl:") && !param.startsWith("gopher:") && !param.startsWith("ftp:") && !param.startsWith("finger:") && !param.startsWith("file:") && !param.startsWith("data:") && !param.startsWith("clsid:") && !param.startsWith("md5:") && !param.startsWith("mailserver:") && !param.startsWith("mailto:") && !param.startsWith("mid:") && !param.startsWith("news:") && !param.startsWith("nntp:") && !param.startsWith("path:") && !param.startsWith("prospero:") && !param.startsWith("service:") && !param.startsWith("shttp") && !param.startsWith("snews") && !param.startsWith("STANF:") && !param.startsWith("telnet:") && !param.startsWith("vemmi:") && !param.startsWith("wais:") && !param.startsWith("whois++:")) {
  170.          param = "http://" + param;
  171.       }
  172.  
  173.       return param;
  174.    }
  175.  
  176.    private void getNumButtons() {
  177.       int i;
  178.       for(i = 0; ((Applet)this).getParameter("ButtonText" + i) != null; ++i) {
  179.       }
  180.  
  181.       this.numButtons = i;
  182.    }
  183.  
  184.    private void parseDocumentBase() {
  185.       String fullBase = ((Applet)this).getDocumentBase().toString();
  186.       this.documentBase = fullBase.substring(0, fullBase.lastIndexOf(47));
  187.    }
  188.  
  189.    protected URL[] getURLList(int buttonNum) {
  190.       int subCount = this.countSubEntries(buttonNum);
  191.       URL[] URLList = new URL[subCount];
  192.  
  193.       for(int i = 0; i < subCount; ++i) {
  194.          String theUrl = this.modifyStringContext(((Applet)this).getParameter("URL" + buttonNum + "_" + i));
  195.  
  196.          try {
  197.             URLList[i] = new URL(theUrl);
  198.          } catch (Exception var8) {
  199.             URLList[i] = null;
  200.          }
  201.       }
  202.  
  203.       return URLList;
  204.    }
  205.  
  206.    protected void setCorrectLayout() {
  207.       ((Container)this).setLayout((LayoutManager)null);
  208.    }
  209.  
  210.    public void actionMouseExit(PopObject message) {
  211.    }
  212.  
  213.    protected String[] getStringList(int buttonNum) {
  214.       int subCount = this.countSubEntries(buttonNum);
  215.       String[] stringList = new String[subCount];
  216.  
  217.       for(int i = 0; i < subCount; ++i) {
  218.          String param = ((Applet)this).getParameter("ButtonText" + buttonNum + "_" + i);
  219.          stringList[i] = param != null ? param : " ";
  220.       }
  221.  
  222.       return stringList;
  223.    }
  224.  
  225.    protected void getBackgroundColor() {
  226.       int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  227.       backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  228.       this.backgroundColor = new Color(backgroundcolor);
  229.    }
  230.  
  231.    private void setBackgroundMode() {
  232.       if (this.backgroundColor != null) {
  233.          ((Component)this).setBackground(this.backgroundColor);
  234.       }
  235.  
  236.    }
  237.  
  238.    private void getBackgroundMode() {
  239.       if (((Applet)this).getParameter("backgroundColor") != null) {
  240.          this.getBackgroundColor();
  241.       } else if (((Applet)this).getParameter("backgroundImage") != null) {
  242.          this.getBackgroundImage();
  243.       }
  244.  
  245.    }
  246.  
  247.    protected void getFontColor() {
  248.       int fontcolor = Integer.parseInt(((Applet)this).getParameter("FontColor"));
  249.       fontcolor = this.convertBGRtoRGB(fontcolor);
  250.       this.fontColor = new Color(fontcolor);
  251.       int highlitefontcolor = Integer.parseInt(((Applet)this).getParameter("HighliteFontColor"));
  252.       highlitefontcolor = this.convertBGRtoRGB(highlitefontcolor);
  253.       this.highliteFontColor = new Color(highlitefontcolor);
  254.    }
  255.  
  256.    protected void initButtonInfo() {
  257.       this.getNumButtons();
  258.       this.getButtonImages();
  259.       this.getButtonFont();
  260.       this.getFontColor();
  261.       this.getTextJustification();
  262.       this.getTextAlignment();
  263.    }
  264.  
  265.    private void initAppletInfo() {
  266.       this.parseDocumentBase();
  267.       this.getOrientation();
  268.       this.getBackgroundMode();
  269.       this.setBackgroundMode();
  270.       this.setCorrectLayout();
  271.    }
  272.  
  273.    public void actionMouseEnter(PopObject message) {
  274.    }
  275.  
  276.    protected void action(PopObject message) {
  277.       switch (message.getMessageType()) {
  278.          case 0:
  279.             this.actionMouseExit(message);
  280.             break;
  281.          case 1:
  282.             this.actionMouseEnter(message);
  283.             break;
  284.          case 2:
  285.             this.actionMouseDown(message);
  286.       }
  287.  
  288.    }
  289.  
  290.    private int convertBGRtoRGB(int BGRColor) {
  291.       int r = (BGRColor & 255) << 16;
  292.       int g = BGRColor & '\uff00';
  293.       int b = (BGRColor & 16711680) >> 16;
  294.       return r + g + b;
  295.    }
  296.  
  297.    protected URL getButtonURL(int buttonNum) {
  298.       URL url;
  299.       try {
  300.          String param = this.modifyStringContext(((Applet)this).getParameter("URL" + buttonNum));
  301.          url = new URL(param);
  302.       } catch (Exception var5) {
  303.          url = null;
  304.       }
  305.  
  306.       return url;
  307.    }
  308.  
  309.    public void init() {
  310.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  311.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  312.       this.initAppletInfo();
  313.       this.initButtonInfo();
  314.  
  315.       for(int i = 0; i < this.numButtons; ++i) {
  316.          String param = ((Applet)this).getParameter("ButtonText" + i);
  317.          String buttonText = param != null ? param : " ";
  318.          URL buttonURL = this.getButtonURL(i);
  319.          URL[] URLList = this.getURLList(i);
  320.          String[] stringList = this.getStringList(i);
  321.          PopObject popObject = new PopObject(i, buttonURL, URLList, stringList);
  322.          PopButton aButton;
  323.          if (this.backgroundColor != null) {
  324.             aButton = new PopButton(this, popObject, buttonText, this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundColor);
  325.          } else {
  326.             aButton = new PopButton(this, popObject, buttonText, this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundImage);
  327.          }
  328.  
  329.          if (this.orientation == 0) {
  330.             ((Component)aButton).reshape(0, i * this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT);
  331.          } else {
  332.             ((Component)aButton).reshape(i * this.BUTTON_WIDTH, 0, this.BUTTON_WIDTH, this.BUTTON_HEIGHT);
  333.          }
  334.  
  335.          ((Container)this).add(aButton);
  336.       }
  337.  
  338.    }
  339.  
  340.    protected void getButtonFont() {
  341.       int fontSize = Integer.parseInt(((Applet)this).getParameter("FontSize"));
  342.       String buttonfont = ((Applet)this).getParameter("ButtonFont");
  343.       int fontStyle = 0;
  344.       if (((Applet)this).getParameter("Bold") != null && ((Applet)this).getParameter("Italic") != null) {
  345.          fontStyle = 3;
  346.       } else if (((Applet)this).getParameter("Bold") != null) {
  347.          fontStyle = 1;
  348.       } else if (((Applet)this).getParameter("Italic") != null) {
  349.          fontStyle = 2;
  350.       }
  351.  
  352.       fontSize += 3;
  353.       this.buttonFont = new Font(buttonfont, fontStyle, fontSize);
  354.    }
  355.  
  356.    private void getOrientation() {
  357.       if (((Applet)this).getParameter("Orientation").equals("Horizontal")) {
  358.          this.orientation = 1;
  359.       } else {
  360.          this.orientation = 0;
  361.       }
  362.  
  363.    }
  364. }
  365.